# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :condition_test.py
# @Time      :2025/7/6 15:07
# @Author    :aiyimu

import threading

shared_resource = [] # 共享资源

def consumer(cond):
    with cond:
        cond.wait()
        print("消费者准备消费...")
        for i in range(3):
            print(f"消费者消费了{i}")
            shared_resource.remove(i)

def producer(cond):
    with cond:
        print("生产者准备生产...")
        for i in range(3):
            print(f"生产者生产了{i}")
            shared_resource.append(i)
        cond.notify() # 通知消费者

condition = threading.Condition()
c = threading.Thread(name="consumer", target=consumer, args=(condition,))
p = threading.Thread(name="producer", target=producer, args=(condition,))

c.start()
p.start()